<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>bucketSort</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const bucketSort = (arr, size = 5) => {
            const min = Math.min(...arr);
            const max = Math.max(...arr);
            const buckets = Array.from(
                { length: Math.floor((max - min) / size) + 1 },
                () => []
            );
            arr.forEach(val => {
                buckets[Math.floor((val - min) / size)].push(val);
            });
            return buckets.reduce((acc, b) => [...acc, ...b.sort((a, b) => a - b)], []);
        };
        console.log(bucketSort([6, 3, 4, 1]));
    </script>
</body>

</html>